package cn.edu.ccut.refactor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

import cn.edu.ccut.util.RefactorXMLParse;

/**
 * 
 * @author Bai Yucheng 2008-1-10
 * 
 */
public class RenameSaturnFileChange extends Change {
	private IFile saturnFile;
	private IPath fSaturnFilePath;
	private String fSaturnNewName;

	public RenameSaturnFileChange(IFile file, String newName) {
		this(file.getFullPath(), newName);
		setSaturnFile(file);
	}

	private RenameSaturnFileChange(IPath ipath, String newName) {
		fSaturnFilePath = ipath;
		fSaturnNewName = newName;
	}

	private IResource getResource() {
		return ResourcesPlugin.getWorkspace().getRoot().findMember(fSaturnFilePath);
	}

	public RefactoringStatus isValid(IProgressMonitor iprogressmonitor)
			throws CoreException {
		return null;
	}

	private String getDebugFileName(String s) {
		if (s.indexOf('.') == -1)
			return s.concat(".xml"); //$NON-NLS-1$
		else
			return s.substring(0, s.indexOf('.')) + ".xml"; //$NON-NLS-1$
	}

	public Change perform(IProgressMonitor iprogressmonitor)
			throws CoreException {
		RenameSaturnFileChange renameSaturnFileChange = null;
		try {
			iprogressmonitor.beginTask(Messages.getString("RenameSaturnFileChange.2"), 1); //$NON-NLS-1$
			if (saturnFile.getFileExtension().equals("saturndata")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				RefactorXMLParse.renameSelfName(saturnFile, fSaturnNewName, 0);
				refreshOutputDir("\\" + saturnFile.getProject().getName() + "\\");  //$NON-NLS-1$ //$NON-NLS-2$
			}
			IFile file = getSaturnFile();
			getResource().move(renamedResourcePath(fSaturnFilePath, fSaturnNewName),
					getCoreRenameFlags(), iprogressmonitor);
			RefactorUtil.renameFile(file, getDebugFileName(fSaturnNewName));
			String s = fSaturnFilePath.lastSegment();
			IPath ipath = renamedResourcePath(fSaturnFilePath, fSaturnNewName);
			renameSaturnFileChange = new RenameSaturnFileChange(ipath, s);
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			iprogressmonitor.done();
		}
		return renameSaturnFileChange;
	}
	
	private static void refreshOutputDir(String outputdir) {
		IResource bufferRes = findMember(ResourcesPlugin.getWorkspace().getRoot(), outputdir);

		if (bufferRes != null && bufferRes.isAccessible()) {
			try {
				bufferRes.refreshLocal(IResource.DEPTH_INFINITE, null);
			} catch (CoreException e) {
			}
		}
	}

	private static IResource findMember(IWorkspaceRoot root, String path) {
		Path pathOrNull = pathOrNull(path);
		if (pathOrNull == null)
			return null;
		return root.findMember(pathOrNull);
	}

	private static Path pathOrNull(String p) {
		if (p == null || p.trim().length() == 0) {
			return null;
		} else {
			return new Path(p);
		}
	}

	private int getCoreRenameFlags() {
		IResource iresource = getResource();
		if (iresource == null)
			return 0;
		return !iresource.isLinked() ? 0 : 32;
	}

	private static IPath renamedResourcePath(IPath ipath, String s) {
		return ipath.removeLastSegments(1).append(s);
	}

	public Object getModifiedElement() {
		return getResource();
	}

	public IFile getSaturnFile() {
		return saturnFile;
	}

	public void setSaturnFile(IFile saturnFile) {
		this.saturnFile = saturnFile;
	}

	@Override
	public String getName() {
		return "" + fSaturnNewName; //$NON-NLS-1$
	}

	@Override
	public void initializeValidationData(IProgressMonitor pm) {
	}
}
